// pages/userInfoCenter/index.js
const App = getApp();
const { Dialog } = require("../../../../config/modal")
const utils = require("../../../../utils/util")
const { LOGIN, LOCATION } = require("../../../../config/login.js")
Page({
    /**
     * 页面的初始数据
     */
    data: {
        theamTextColor: App.globalData.theamTextColor,
        userInfo: {
            realName: "",
            invitees: "",
            alipayAccount: "",
        },
        integral: {
            ruleShow: false,
            isCurrentStatus: false,
            rules: {
                singIn: 2,
                keepDay: 15,
                keepSign: 15,
                tip: '',
                ruleRemark: '',
                minCash: 100,
                maxCash: 200,
            },
            html: ' <h1 style="color: red">Title</h1>',
            amount: 0,
            usedIntegral: 0,
            rollRecords: []
        }
        // 提现金额
        , brush: {
            show: false
            , amount: '',
            resultTip: "",
        }
    },
    URL: {
        getIntegral: "/integral/getIntegral",
        sign: "/integral/signIn",
        rules: "/integral/rules",
        withdrawal: "/integral/withdrawal",
        getRollRecords: "/integral/getRollRecords"

    },
    page: {
        detail: "/pages/tabbar/user/integral/detail/index",
        userCenter: "/pages/tabbar/user/userInfoCenter/index",
        records: "/pages/tabbar/user/integral/exchange/records/index",
        home: "/pages/tabbar/index/index",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        utils.saveFromPage(App, getCurrentPages());
    },
    onShow() {
        this.initUser();
        this.initIntegral();
        this.initIntegralRule();
        this.getRollRecords();
    },
    getRollRecords() {
        App.$get(false, this.URL.getRollRecords, { pageSize: 50 }, res => {
            if (res.R) {
                this.setData({
                    'integral.rollRecords': res.data.rows
                })
            }
        })
    },
    initUser() {
        let userInfo = App.$getUserInfo();
        this.setData({
            'userInfo.realName': userInfo.realName,
            'userInfo.alipayAccount': userInfo.alipayAccount,
            'userInfo.inviteCode': userInfo.inviteCode,
        })
    },
    tabChange(event) {
        wx.showToast({
            title: `${event.detail.name}`,
            icon: 'none',
        });
    },


    // 去兑换中心
    toChangeCenter() {
        App.$navigateTo(this.page.records, true);
    },

    // 查看积分规则
    viewIntegralRules() {
        this.setData({
            'integral.ruleShow': true
        })
    },
    closeRule() {
        this.setData({
            'integral.ruleShow': false
        })
    },

    // 积分流水
    viewIntegralDetail() {
        App.$navigateTo(this.page.detail, true);
    },

    // 分享
    shareApp() {
        App.$alert("功能尚未开放")
    },

    // 签到
    doSign() {
        if (this.data.integral.isCurrentStatus) {
            return;
        }
        App.$post(true, this.URL.sign, {}, res => {
            if (res.R) {
                let signDay = res.data.signDay;
                if (signDay.isKeepSign) {
                    App.$alert(signDay.msg);
                    return;
                }
                this.initIntegral();
                App.$alert("您已连续签到：" + signDay.signDay + "天");
            }
        })
    },

    // 积分提现
    doBrush() {
        this.setData({
            'brush.show': true
        })
        //App.$alert("功能尚未开放")
    }
    , onAmountChange(e) {
        if (utils.isInvalid(e.detail)) {
            this.setData({
                'brush.amount': ""
            })
            return;
        }

        if (!utils.checkIsNum(e.detail)) {
            this.setData({
                'brush.amount': ""
            })
            return;
        }
        if (e.detail < this.data.integral.rules.minCash || e.detail > this.data.integral.rules.maxCash) {
            this.setData({
                'brush.tip': "积分范围在[ " + this.data.integral.rules.minCash + ' - ' + this.data.integral.rules.maxCash + " ]"
            })
            return;
        }
        this.setData({
            'brush.tip': "",
            'brush.amount': e.detail
        })
    }
    , closeBrush(e) {
        if (e.detail === 'confirm') {
            this.setData({
                'brush.show': true
            })
            return;
        }
    }

    // 提交请求
    , toBrush() {
        if (utils.isInvalid(this.data.brush.amount)) {
            this.setData({
                'brush.tip': "请输入提现积分"
            })
            return;
        }

        if (this.data.brush.amount < this.data.integral.rules.minCash || this.data.brush.amount > this.data.integral.rules.maxCash) {
            return;
        }

        if (utils.isInvalid(this.data.userInfo.alipayAccount)) {
            return;
        }

        if (this.data.brush.amount < this.data.integral.rules.minCash || this.data.brush.amount > this.data.integral.rules.maxCash) {
            return;
        }

        App.$postIsTip(true, false, this.URL.withdrawal, { amount: this.data.brush.amount }, res => {
            if (res.R) {
                this.setData({
                    'brush.show': false
                })
                App.$alert(res.msg);
                this.initIntegral();
                return;
            }
            App.$alert(res.msg)
        })
    }

    // 获取积分
    , initIntegral() {
        App.$get(true, this.URL.getIntegral, {}, res => {
            if (res.R) {
                this.setData({
                    "integral.amount": res.data.integral,
                    "integral.isCurrentStatus": res.data.currentDaySign,
                    "integral.usedIntegral": res.data.usedIntegral,
                    "integral.keepSignDay": res.data.keepSignDay,
                })
            }
        })
    },
    // 积分规则说明
    initIntegralRule() {
        App.$get(true, this.URL.rules, {}, res => {
            if (res.R) {
                this.setData({
                    "integral.rules": res.data
                })
            }
        })
    },

    goBack() {
        App.$redirectTo(this.page.userCenter, true);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        console.log("分享给他人,我的邀请码", this.data.userInfo.inviteCode)
        return {
            title: "校园CBD",
            path: this.page.home + '?invitees=' + this.data.userInfo.inviteCode,
        }
    }
})
